home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / lib / mathlib / libconv / EXAMPLES / cex3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.2 KB  |  80 lines

  1. /* ***************************************************************************
  2. *                                                                            *
  3. *  This program is to illustrate the use of 2D and Multiple 1D filters       *
  4. *  We use here DFIR2D and DFIRM1D modules from LIBCONV                       *
  5. *                                                                            *
  6. ******************************************************************************
  7. *                                                                            *
  8. *  The 1D filter in the X direction will be (1/2,-1.,1/2)                    *
  9. *                               --- Approximation of second derivative       *
  10. *  In the Y direction the 1D filter will be (-1/2,0.,1/2)                    *
  11. *                               --- Approximation of first  derivative       *
  12. *  The equivalent 2D filter will be:                                         *
  13. *                               -1/4   0   +1/4                              *
  14. *                               +1/2   0   -1/2                              *
  15. *                               -1/4   0   +1/4                              *
  16. *                               ------ Y ----->                              *
  17. *  This is the approximation of the triple derivative D()/Dxxy               *
  18. *                                                                            *
  19. *************************************************************************** */
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include "conv.h"
  23. void     print_2D_Array( double *input, int nx, int ny);
  24.  
  25. #define HX 5
  26. #define HY 5
  27. #define NX (2*HX+1)
  28. #define NY (2*HY+1)
  29.  
  30. #define SCALE_X  (M_PI/(double)HX)
  31. #define SCALE_Y  (M_PI/(double)HY)
  32. #define ABS(a) (((a) > 0) ? (a) : (-(a)))
  33. double fil2d[9]={ -.25,0.5,-.25,
  34.            0.0,0.0,0.0,
  35.            .25,-.5,.25};
  36. double filx[3]={ .5, -1., .5};
  37. double fily[3]={ -.5, 0.0, 0.5};
  38. main() {
  39.     double input[NX*NY], out1[NX*NY], out2[NX*NY], temp[NX*NY];
  40.     int i, j;
  41.     for( j = -HY ; j <= HY ; j++) {
  42.     for( i = -HX ; i <= HX ; i++) {
  43.         input[(j+HY)*NX+(i+HX)] =
  44.         cos((double)i*SCALE_X)*(1.-((double)ABS(i))/(double)HX) *
  45.         cos((double)j*SCALE_Y)*(1.-((double)ABS(j))/(double)HY);
  46.     }
  47.     }
  48. /*  2D FIR filter:  */
  49.     dfir2d( input,1,NX,-HX,NX,-HY,NY, fil2d,1,3, -1,  3, -1, 3,
  50.         out1, 1,NX,-HX,NX,-HY,NY, 1.0, 0.0);
  51.  
  52. /*  Multiple 1D FIR filter in the X direction:  */
  53.     dfirm1d(    input,1,NX,-HX,NX, NY, filx, 1,   -1, 3,
  54.         temp, 1,NX,-HX,NX,       1.0, 0.0);
  55.  
  56. /*  Multiple 1D FIR filter in the Y direction:  */
  57.     dfirm1d(    temp,NX,1,-HY,NY, NX, fily, 1,   -1, 3,
  58.         out2,NX,1,-HY,NY,        1.0, 0.0);
  59.  
  60. /*  Print out the input and the two results  */
  61.     printf("\n Input : \n");
  62.     print_2D_Array( input, NX, NY);
  63.     printf("\n After application of 2D FIR filter: \n");
  64.     print_2D_Array( out1,  NX, NY);
  65.     printf("\n After multiple 1D FIR filters in the X Direction: \n");
  66.     print_2D_Array( temp,  NX, NY);
  67.     printf("\n After final multiple 1D FIR filters in the Y Direction: \n");
  68.     print_2D_Array( out2,  NX, NY);
  69. }
  70. void     print_2D_Array( double *ptr, int nx, int ny){
  71.     int i,j;
  72.     double *this;
  73.     for( j=nx ; j > 0; j--, ptr ++) {
  74.         this = ptr;
  75.     for( i=ny ; i>0 ; i--, this += nx)
  76.         printf(" %6.3f",*this);
  77.     printf("\n");
  78.     }
  79. }
  80.